//
//  GFCollectViewController.m
//  GoodFood
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFCollectViewController.h"

@interface GFCollectViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *deModelArr;
@end

@implementation GFCollectViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [[DataBaseHandle shareDataBaseHandle] openDB];
    
    self.deModelArr = [NSMutableArray arrayWithArray:[[DataBaseHandle shareDataBaseHandle] selectAllFoodDetails]];
    
    if (self.deModelArr.count == 0) {
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"暂无收藏" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];

        
        [alertView show];

        
    }
    
    self.title = @"我的收藏";
    
    UIColor *color = [UIColor colorWithRed:209/255.0 green:186/255.0 blue:116/255.0 alpha:1];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];/* 在NSFontAttributeName中寻找*/
    
    self.navigationController.navigationBar.titleTextAttributes = dic;
    
    [self.tableView reloadData];
    
}

#pragma mark _ 创建tableView
- (void)creatTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
    
    /* 重用池 */
    [self.tableView registerClass:[GFCollectCell class] forCellReuseIdentifier:@"CollectTableViewCell"];
    
    [self.view addSubview:self.tableView];

    
}

#pragma mark _ tableView DataSource 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.deModelArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GFCollectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CollectTableViewCell"];
    
    cell.deModel = [self.deModelArr objectAtIndex:indexPath.row];
    
    return cell;
    
}

#pragma mark _ tableView Delegate 协议方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
   GFDetialsViewController *detailsVC = [[GFDetialsViewController alloc] init];
    
    GFDetialsModel *deModel = [self.deModelArr objectAtIndex:indexPath.row];
    
    detailsVC.food_code = deModel.code;
    
    [self.navigationController pushViewController:detailsVC animated:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.waitView removeFromSuperview];
    //    NSLog(@"%@", self.deModelArr);
    
    [self creatTableView];

    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"清空收藏" style:UIBarButtonItemStylePlain target:self action:@selector(deleteAllFood:)];
    self.navigationItem.rightBarButtonItem = item;
    
    // Do any additional setup after loading the view.
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"houtui1"] style:UIBarButtonItemStylePlain target:self action:@selector(backToLastVC:)];
    self.navigationItem.leftBarButtonItem = backItem;

}

- (void)backToLastVC:(UIBarButtonItem *)item {
    
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)deleteAllFood:(UIBarButtonItem *)item {
    
    [[DataBaseHandle shareDataBaseHandle] dropTable];
    
    self.deModelArr = [NSMutableArray arrayWithArray:[[DataBaseHandle shareDataBaseHandle] selectAllFoodDetails]];
    
    [self.tableView reloadData];
}

#pragma mark - 进图tableView可编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
    [super setEditing:editing animated:animated];
    
    [self.tableView setEditing:editing animated:animated];
    
}

/* 哪些能编辑 */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GFDetialsModel *deModel = [self.deModelArr objectAtIndex:indexPath.row];
    
    [self.deModelArr removeObject:deModel];
    
    [[DataBaseHandle shareDataBaseHandle] deleteFoodDetailsWithFoodName:deModel.code];
    
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
